<?php
class StrukturController extends FrontEndController {
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/dalam';
    /**
     * @return array action filters
     */
    public function filters() {
	return array(
	    'accessControl', // perform access control for CRUD operations
	    'postOnly + delete', // we only allow deletion via POST request
	);
    }
    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
//    public function accessRules()
//    {
//        return array(
//            array(
//                'allow', // allow all users to perform 'index' and 'view' actions
//                'actions' => array('index', 'view'),
//                'users' => array('*'),),
//            array(
//                'allow', // allow authenticated user to perform 'create' and 'update' actions
//                'actions' => array('create', 'update'),
//                'users' => array('@'),),
//            array(
//                'allow', // allow admin user to perform 'admin' and 'delete' actions
//                'actions' => array('admin', 'delete'),
//                'users' => array('admin'),),
//            array(
//                'deny', // deny all users
//                'users' => array('*'),),);
//    }
    /**
     * Displays a particular model.
     *
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id) {
	$this->render('view', array(
	    'model' => $this->loadModel($id),));
    }
    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate() {
	$model = new Struktur;
	// Uncomment the following line if AJAX validation is needed
	// $this->performAjaxValidation($model);
	if (isset($_POST['Struktur'])) {
	    $model->attributes = $_POST['Struktur'];
	    if ($model->save())
		    $this->redirect(array('view', 'id' => $model->id_struktur));
	}
	$this->render('create', array(
	    'model' => $model,));
    }
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id) {
	$model = $this->loadModel($id);
	// Uncomment the following line if AJAX validation is needed
	// $this->performAjaxValidation($model);
	if (isset($_POST['Struktur'])) {
	    $model->attributes = $_POST['Struktur'];
	    if ($model->save())
		    $this->redirect(array('view', 'id' => $model->id_struktur));
	}
	$this->render('update', array(
	    'model' => $model,));
    }
    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     *
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id) {
	$this->loadModel($id)->delete();
	// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
	if (!isset($_GET['ajax']))
		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    protected function build_struktur($id, $var) {
	global $struktur_org;
	$item = '';
	$model = $this->loadModel($id);
	if ($model == null) return $item;
	$item .= "
        var $var = new primitives.orgdiagram.ItemConfig();
    $var.title = '" . $model->laskar0->nama . "';
    $var.description = '" . $struktur_org[$model->kode_lokasi] . "';
    $var.image = 'http://www.basicprimitives.com/demo/images/photos/a.png';
    ";
	$criteria = new CDbCriteria;
	$criteria->addCondition("parent = $id");
	$childs = Struktur::model()->findAll($criteria);
	foreach ($childs as $child) {
	    $child_var = random_string(6);
	    $item .= $this->build_struktur($child->id_struktur, $child_var);
	    $item .= "$var.items.push($child_var);";
	}

	if ($model->kode_lokasi > 100) {
	    for ($i = 0; $i < 3; $i++) {
		$var_kabupaten = random_string(6);
		$item .= $this->generate_struktur($var_kabupaten);
		$item .= "$var.items.push($var_kabupaten);";
	    }
	}

	return $item;
    }
    protected function generate_struktur($var) {
	$item = "
        var $var = new primitives.orgdiagram.ItemConfig();
    $var.title = 'Koor.Kabupaten';
    $var.description = '';
    $var.image = 'http://www.basicprimitives.com/demo/images/photos/a.png';
    ";
	for ($i = 0; $i < 3; $i++) {
	    $vari = random_string(6);
        /*Koor.Kecamatan
          Koor.Kelurahan
          Desa
        */
	    $item .= "
        var $vari = new primitives.orgdiagram.ItemConfig();
    $vari.title = 'kolasmat';
    $vari.description = '';
    $vari.image = 'http://www.basicprimitives.com/demo/images/photos/a.png';
    $var.items.push($vari);
    ";

	    for ($i = 0; $i < 3; $i++) {
		$varik = random_string(6);
		$item .= "
        var $varik = new primitives.orgdiagram.ItemConfig();
    $varik.title = 'kolasrah';
    $varik.description = '';
    $varik.image = 'http://www.basicprimitives.com/demo/images/photos/a.png';
    $vari.items.push($varik);
    ";
	    }

	    for ($x = 0; $x < 3; $x++) {
		$varx = random_string(6);
		$item .= "
        var $varx = new primitives.orgdiagram.ItemConfig();
    $varx.title = 'kolassa';
    $varx.description = '';
    $varx.image = 'http://www.basicprimitives.com/demo/images/photos/a.png';
    $vari.items.push($varx);
    ";
	    }
	}
	return $item;
    }
    /**
     * Lists all models.
     */
    public function actionIndex() {
	$reskab = array();
	$reskec = array();
	$reskel = array();
	$resdesa = array();

	$respengkab = array();
	$respengkec = array();
	$respengkel = array();
	$respengdesa = array();
	// http://www.yiiframework.com/doc/guide/database.dao
	//select kabupaten, kecamatan, dll

	$sqlkab = "SELECT
                    psn_kabupaten.nama_kabupaten,
                    psn_kabupaten.id_kabupaten
                    FROM
                    psn_kabupaten
                    ";
	$commandkab = app()->db->createCommand($sqlkab);
	$reskab = $commandkab->queryAll();

	$sqlkec = "SELECT
                    psn_kecamatan.nama_kecamatan,
                    psn_kecamatan.id_kecamatan
                    FROM
                    psn_kecamatan";
	$commandkec = app()->db->createCommand($sqlkec);
	$reskec = $commandkec->queryAll();

	$sqlkel = "SELECT
                    psn_kelurahan.nama_kelurahan,
                    psn_kelurahan.id_kelurahan
                    FROM
                    psn_kelurahan";
	$commandkel = app()->db->createCommand($sqlkel);
	$reskel = $commandkel->queryAll();

	$sqldes = "SELECT
                    psn_desa.nama_desa,
                    psn_desa.id_desa
                    FROM
                    psn_desa";
	$commanddes = app()->db->createCommand($sqldes);
	$resdes = $commanddes->queryAll();



	//select pengurus

	$sqlpengkab = "SELECT
                    psn_kabupaten.id_kabupaten,
                    psn_kabupaten.nama_kabupaten,
                    psn_kabupaten_anggota.jabatan,
                    psn_jamaah.nama
                    FROM
                    psn_kabupaten_anggota
                    INNER JOIN psn_jamaah ON psn_kabupaten_anggota.id_laskar = psn_jamaah.id_laskar
                    right JOIN psn_kabupaten ON psn_kabupaten_anggota.id_kabupaten = psn_kabupaten.id_kabupaten
                    order by psn_kabupaten.nama_kabupaten asc,   psn_kabupaten_anggota.jabatan asc
                    ";
	$commandpengkab = app()->db->createCommand($sqlpengkab);
	$respengkab = $commandpengkab->queryAll();

	$sqlpengkec = "SELECT
                    psn_kecamatan.id_kecamatan,
                    psn_kecamatan.nama_kecamatan,
                    psn_kecamatan_anggota.jabatan,
                    psn_jamaah.nama
                    FROM
                    psn_kecamatan_anggota
                    INNER JOIN psn_jamaah ON psn_kecamatan_anggota.id_laskar = psn_jamaah.id_laskar
                    right JOIN psn_kecamatan ON psn_kecamatan_anggota.id_kecamatan = psn_kecamatan.id_kecamatan
                     order by psn_kecamatan.nama_kecamatan asc,   psn_kecamatan_anggota.jabatan asc
                   ";
	$commandpengkec = app()->db->createCommand($sqlpengkec);
	$respengkec = $commandpengkec->queryAll();

	$sqlpengkel = "SELECT
                    psn_kelurahan.id_kelurahan,
                    psn_kelurahan.nama_kelurahan,
                    psn_kelurahan_anggota.jabatan,
                    psn_jamaah.nama
                    FROM
                    psn_kelurahan_anggota
                    INNER JOIN psn_jamaah ON psn_kelurahan_anggota.id_laskar = psn_jamaah.id_laskar
                    right JOIN psn_kelurahan ON psn_kelurahan_anggota.id_kelurahan = psn_kelurahan.id_kelurahan
                     order by psn_kelurahan.nama_kelurahan asc,   psn_kelurahan_anggota.jabatan asc
                    ";
	$commandpengkel = app()->db->createCommand($sqlpengkel);
	$respengkel = $commandpengkel->queryAll();

	$sqlpengdes = "SELECT
                    psn_desa.id_desa,
                    psn_desa.nama_desa,
                    psn_desa_anggota.jabatan,
                    psn_jamaah.nama
                    FROM
                    psn_desa_anggota
                    INNER JOIN psn_jamaah ON psn_desa_anggota.id_laskar = psn_jamaah.id_laskar
                    right JOIN psn_desa ON psn_desa_anggota.id_desa= psn_desa.id_desa
                    order by  psn_desa.nama_desa asc,   psn_desa_anggota.jabatan asc
                    ";
	$commandpengdes = app()->db->createCommand($sqlpengdes);
	$respengdes = $commandpengdes->queryAll();


	$rootItem = 'rootItem';
	$struktur = $this->build_struktur(1, $rootItem);
	$script = "
        $(window).load(function(){
            var options = new primitives.orgdiagram.Config();
            $struktur
            options.rootItem = $rootItem;
            options.hasSelectorCheckbox = primitives.common.Enabled.False;
            jQuery('.basicdiagram').orgDiagram(options);

        });";
	$dataProvider = new CActiveDataProvider('Struktur');
	$this->render('index',
		array(
	    'dataProvider' => $dataProvider,
	    'script' => $script,
	    'reskab' => $reskab,
	    'reskec' => $reskec,
	    'reskel' => $reskel,
	    'resdes' => $resdes,
	    'respengkab' => $respengkab,
	    'respengkec' => $respengkec,
	    'respengkel' => $respengkel,
	    'respengdes' => $respengdes,
	));
    }
    /**
     * Manages all models.
     */
    public function actionAdmin() {
	$model = new Struktur('search');
	$model->unsetAttributes(); // clear any default values
	if (isset($_GET['Struktur'])) $model->attributes = $_GET['Struktur'];
	$this->render('admin', array(
	    'model' => $model,));
    }
    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     *
     * @param integer $id the ID of the model to be loaded
     *
     * @return Struktur the loaded model
     * @throws CHttpException
     */
    public function loadModel($id) {
	$model = Struktur::model()->findByPk($id);
	if ($model === null)
		throw new CHttpException(404, 'The requested page does not exist.');
	return $model;
    }
    /**
     * Performs the AJAX validation.
     *
     * @param Struktur $model the model to be validated
     */
    protected function performAjaxValidation($model) {
	if (isset($_POST['ajax']) && $_POST['ajax'] === 'struktur-form') {
	    echo CActiveForm::validate($model);
	    Yii::app()->end();
	}
    }
}
